<script setup>
import { onMounted, ref } from 'vue';
import { showApi, showBackApi, delApi } from '@/api/area'
import modify from '@/views/area/modify.vue'
import { ElMessage } from 'element-plus';
const tableData = ref()
const mref = ref('mref')
const getList = async () => {
  let res = await showApi()
  tableData.value = res
}
const handleAdd = async (id) => {
  mref.value.titleName = '新增'
  mref.value.isAdd = true
  mref.value.dialogVisible = true
  let res = await showBackApi(id)
  mref.value.dialogFormData.parentId = res.data.id
}
const handleEdit = async (id) => {
  mref.value.titleName = '修改'
  mref.value.isAdd = false
  mref.value.dialogVisible = true
  let res = await showBackApi(id)
  mref.value.dialogFormData = res.data
}
const ids = ref([])
const handleDelete = async (id) => {
  ids.value = id
  let res = await delApi(ids.value)
  if (res.code) {
    ElMessage.success('操作成功!')
  } else {
    ElMessage.error('操作失败！')
  }
  getList()
}
onMounted(() => {
  getList()
})
</script>
<template>
  <modify ref="mref" @search="getList()" />
  <div>
    <el-table :data="tableData" style="width: 100%" :expand-row-keys="expandedKeys" row-key="id"
      @expand-change="handleExpandChange">
      <el-table-column type="index" label="#" width="50" fixed />
      <el-table-column prop="name" label="区域">
        <template #default="{ row, $index }">
          <span v-if="!row.children || row.children.length === 0">
            {{ row.name }}
          </span>
          <span v-else>
            <span>{{ row.name }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column fixed="right" label="操作" width="200px">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="handleAdd()">新增</el-button>
          <el-button type="primary" size="small" @click="handleEdit(row.id)">修改</el-button>
          <el-button type="danger" size="small" @click="handleDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>